import { z } from 'zod'

// 自定义错误消息
const errorMessages = {
  required: '此字段为必填项',
  invalidUrl: '请输入有效的URL地址',
  invalidEmail: '请输入有效的邮箱地址',
  minLength: (min) => `至少需要 ${min} 个字符`,
  invalidNumber: '请输入有效的数字',
  invalidArray: '必须是数组类型',
  arrayMinLength: (min) => `至少需要 ${min} 个项目`,
  invalidCategory: '无效的项目分类'
}

// 通用验证规则
const commonRules = {
  url: z.string().url(errorMessages.invalidUrl),
  nonEmptyString: z.string().min(1, errorMessages.required),
  numberArray: z.array(z.number()).min(1, errorMessages.arrayMinLength(1)),
  stringArray: z.array(z.string()).min(1, errorMessages.arrayMinLength(1))
}

// 项目分类枚举
const ProjectCategory = z.enum(['前端', '后端', '全栈', '其他', 'UI设计'])

const configSchema = z.object({
  basic: z.object({
    name: commonRules.nonEmptyString,
    englishName: commonRules.nonEmptyString,
    title: commonRules.nonEmptyString,
    motto: commonRules.nonEmptyString,
    location: commonRules.nonEmptyString,
    email: z.string().email(errorMessages.invalidEmail),
    resumeLink: z.string()
  }).strict(),
  
  social: z.object({
    github: z.string(),
    linkedin: z.string(),
    blog: z.string()
  }).strict(),
  
  workExperience: z.array(z.object({
    id: z.number(errorMessages.invalidNumber),
    period: commonRules.nonEmptyString,
    title: commonRules.nonEmptyString,
    company: commonRules.nonEmptyString,
    achievements: z.array(z.string())
  })),
  
  projects: z.array(z.object({
    id: z.number(errorMessages.invalidNumber),
    title: commonRules.nonEmptyString,
    description: z.string(),
    image: z.string(),
    category: z.string(),
    technologies: z.array(z.string()),
    demo: z.string(),
    github: z.string()
  })),
  
  codeSnippets: z.array(z.object({
    id: z.number(),
    title: z.string(),
    description: z.string(),
    language: z.string(),
    code: z.string(),
    tags: z.array(z.string()),
    version: z.string()
  })).optional(),
  
  education: z.array(z.object({
    period: z.string(),
    degree: z.string(),
    school: z.string(),
    description: z.string()
  })).optional(),
  
  skills: z.object({
    frontend: z.array(z.object({
      name: z.string(),
      level: z.number()
    })),
    backend: z.array(z.object({
      name: z.string(),
      level: z.number()
    }))
  }).optional(),
  
  interests: z.array(z.object({
    icon: z.string(),
    title: z.string(),
    description: z.string()
  })).optional()
})

// 格式化验证错误
function formatValidationError(error) {
  return error.errors.map(err => ({
    path: err.path.join('.'),
    message: err.message,
    code: err.code
  }))
}

// 验证配置
export function validateConfig(config) {
  try {
    configSchema.parse(config)
    return { valid: true, errors: null }
  } catch (err) {
    const formattedErrors = formatValidationError(err)
    console.error('Config validation failed:', formattedErrors)
    return { valid: false, errors: formattedErrors }
  }
}

// 验证单个配置项
export function validateConfigItem(path, value) {
  try {
    const schema = getSchemaByPath(path)
    schema.parse(value)
    return { valid: true, errors: null }
  } catch (err) {
    const formattedErrors = formatValidationError(err)
    return { valid: false, errors: formattedErrors }
  }
}

// 根据路径获取验证规则
function getSchemaByPath(path) {
  const parts = path.split('.')
  let schema = configSchema
  for (const part of parts) {
    schema = schema.shape[part]
  }
  return schema
} 